package com.quanyan.runningriding.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.runningriding.entity.db.*;
import com.quanyan.runningriding.entity.vo.run.resp.MusicDetails;
import com.quanyan.runningriding.mapper.RunMusicDetailsMapper;
import com.quanyan.runningriding.mapper.TbRunMusicCollectOperateRecordMapper;
import com.quanyan.runningriding.mapper.TbRunMusicCollectionMapper;
import com.quanyan.runningriding.mapper.TbRunMusicLibraryMapper;
import com.quanyan.runningriding.service.RunMusicDetailsService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by liubin on 2016/12/15.
 */

@Service
public class RunMusicDetailsServiceImpl implements RunMusicDetailsService{
    @Autowired
    private TbRunMusicCollectionMapper tbRunMusicCollectionMapper;

    @Autowired
    private TbRunMusicLibraryMapper tbRunMusicLibraryMapper;

    @Autowired
    private TbRunMusicCollectOperateRecordMapper tbRunMusicCollectOperateRecordMapper;

    @Override
    public APIResponse<MusicDetails> musicDetailsList(Map<String, Object> paramMap) {
        Integer musicCollectionId = MapUtils.getInteger(paramMap, "musicCollectionId");
        String userId = MapUtils.getString(paramMap, "userId");
        TbRunMusicCollectOperateRecordExample tbRunMusicCollectOperateRecordExample1 = new TbRunMusicCollectOperateRecordExample();
        tbRunMusicCollectOperateRecordExample1.createCriteria().andUserIdEqualTo(userId);
        List<TbRunMusicCollectOperateRecord> tbRunUsers = tbRunMusicCollectOperateRecordMapper.selectByExample(tbRunMusicCollectOperateRecordExample1);
        if (tbRunUsers.isEmpty()){
            return APIResponse.returnFail("用户id不存在");
        }

        MusicDetails musicDetails=new MusicDetails();


        TbRunMusicCollection tbRunMusicCollection=tbRunMusicCollectionMapper.selectByPrimaryKey(musicCollectionId);
        musicDetails.setImgUrl(tbRunMusicCollection.getImgUrl());
        musicDetails.setMusicCollectionId(musicCollectionId);
        musicDetails.setName(tbRunMusicCollection.getMusicCollectionName());
        musicDetails.setDescription(tbRunMusicCollection.getDescription());
        musicDetails.setMusicBpm(tbRunMusicCollection.getMusicBpm());
        
        //统计喜欢的人数
        TbRunMusicCollectOperateRecordExample tbRunMusicCollectOperateRecordExample=new TbRunMusicCollectOperateRecordExample();
        TbRunMusicCollectOperateRecordExample.Criteria criteria=tbRunMusicCollectOperateRecordExample.createCriteria()
                .andMusicCollectionIdEqualTo(musicCollectionId)
                .andOperateTypeEqualTo((byte)1)
                .andOperateActionEqualTo((byte)1);
        Integer loveNum=tbRunMusicCollectOperateRecordMapper.countByExample(tbRunMusicCollectOperateRecordExample);
        musicDetails.setLoveNum(loveNum);

        //统计歌单的歌曲数量
        TbRunMusicLibraryExample tbRunMusicLibraryExample =  new TbRunMusicLibraryExample();
        TbRunMusicLibraryExample.Criteria criteria1 = tbRunMusicLibraryExample.createCriteria()
                .andMusicCollectionIdEqualTo(musicCollectionId);
        Integer songNum = tbRunMusicLibraryMapper.countByExample(tbRunMusicLibraryExample);
        musicDetails.setSongNum(songNum);

        //获取用户喜欢、缓存的状态
        criteria.andUserIdEqualTo(userId);
        Integer isLove=tbRunMusicCollectOperateRecordMapper.countByExample(tbRunMusicCollectOperateRecordExample);
        if (isLove>0) {
            musicDetails.setIsLove((byte)1);
        } else {
            musicDetails.setIsLove((byte)0);
        }
        //清除条件
        tbRunMusicCollectOperateRecordExample.clear();
        tbRunMusicCollectOperateRecordExample.createCriteria().andMusicCollectionIdEqualTo(musicCollectionId)
                .andOperateTypeEqualTo((byte) 3)
                .andOperateActionEqualTo((byte) 1)
                .andUserIdEqualTo(userId);
        Integer isCach=tbRunMusicCollectOperateRecordMapper.countByExample(tbRunMusicCollectOperateRecordExample);
        if (isCach > 0) {
            musicDetails.setIsCach((byte) 1);
        } else {
            musicDetails.setIsCach((byte)0);
        }

        //音乐集合

        List<TbRunMusicLibrary> tbRunMusicLibraries =tbRunMusicLibraryMapper.selectByExample(tbRunMusicLibraryExample);
        musicDetails.setMusicVos(tbRunMusicLibraries);

        return APIResponse.returnSuccess(musicDetails);
    }

}
